/**
 * Created by Jackie.Chen on 2016/12/4.
 */

var Person = function () {
    /*基础配置*/
    var el = 'body';
    var isEdit = false;
    //var userInfo = [[${session.userInfo}]];

    /*获取基本信息*/
    function getUserInfo() {
            if(userInfo.headphoto){
                $('#headphoto').attr('src',userInfo.headphoto);
            }
            $('#username').text(userInfo.username);
            $('#token').text(userInfo.tokenValue);
        var d = new Date(userInfo.lastLoginTime);
        youWant=d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
        $('#loginTime').text(youWant);
    }
    function choose() {
        if(isEdit){
            isEdit = false;
            $('.choose-item').eq(0).find('li').each(function(){
                var $this = $(this);
                if($this.children().hasClass('ico1')){
                    $this.find('.ico1').remove();
                }
            });
        }else{
            isEdit = true;
            $('.choose-item').eq(0).find('li').each(function(){
                var $this = $(this);
                if(!$this.children().hasClass('ico1')) {
                    $this.append('<span class="ico1"></span>');
                }
            });
        }
    }
    /*删除兴趣*/
    function delEnt(){
        var $this = $(this);
        $this.parents('li').remove();
        var entName = $this.siblings('span').eq(0).text();
        $.each($('#chooseEnt li'),function(){
            var me = $(this);
            if(me.find('span').eq(0).text() == entName){
                me.removeClass('ent-br');
            }
        });
    }
    function chooseEnt(){
        if(isEdit){
            var $this = $(this);
            $this.click(function() {
                if (!$this.is('.ent-br')) {
                    $this.addClass('ent-br');
                    $('.choose-item').eq(0).append('<li class="ent-br" data="'+ $this.attr("data")+'"><span class="ent-name" title="'+$this.find('span').eq(0).text()+'">' + $this.find('span').eq(0).text() + '</span><span class="ico1"></span></li>');
                }
            });
        }else{
            layer.msg("请先切换到编辑状态！");
        }
    }

    function confirm() {
        if(checkLogin() == false) {
            toLogin();//未登录跳转到登录页面
        } else {
            if(isEdit){
                var name = "";
                $('.choose-item').eq(0).find('li').each(function () {
                    var $this = $(this);
                    name += $this.attr('data') + ',';
                });
                if(name == ""){
                    layer.msg("请至少选择一项感兴趣的行业！");
                    return;
                }
                var dataparam = {
                    "userid": userInfo.userid,
                    "intrestIndus": name
                };
                ajaxCommonFun({
                    type: 'POST',
                    url: '/user/updateUserInfo.do',
                    data: dataparam,
                    success: function (t) {
                        if (t.status == 'ERROR') {
                            layer.msg(t.message);
                        } else {
                            layer.msg(t.result);
                            isEdit= false;
                            $('.choose-item').eq(0).find('li').each(function(){
                                var $this = $(this);
                                if($this.children().hasClass('ico1')){
                                    $this.find('.ico1').remove();
                                }
                            });
                        }
                    }
                });
            }else{
                layer.msg("请先切换到编辑状态！");
            }
        }
     }
    /*end分页*/
    /*图片上传*/
   function uploadImg(field) {
       if($(this).val() != '' && $(this).val() != null){
           var $field = $(field.target);
           var $parent = $field.parent();
           var $form = $('<form enctype="multipart/form-data" style="display:none"></form>').append($field);
           $('body').append($form);
           loading = layer.load(1, {
               shade: [0.1,'#fff'] //0.1透明度的白色背景
           });
           $form.ajaxSubmit({
               type: 'POST',
               url: '/file/upload/image/upfile.json',
               dataType:"json",
               success: function (t) {
                   layer.close(loading);
                   $form.remove();
                   if(t.responseDATA.rtnCode=='000000'){
                       $parent.find('img').attr('src',t.responseDATA.keys[0].originUrl);
                       $parent.find('img').before($field);
                   }else{
                       layer.msg("上传失败");
                       $parent.append(field);
                   }
               },
               error: function(){
                   layer.close(loading);
                   $parent.prepend(field);
               }
           });
       }
   }

    /*页面初始化和事件绑定*/
    function bindEvents() {
        $(el).undelegate()
            .delegate('.ico1','click',delEnt)
            .delegate('#confirm','click',confirm)
            .delegate('input[type=file]','change',"headPortraitUrl",uploadImg)
            .delegate('#chooseEnt li','click',chooseEnt)
            .delegate('#edit','click',choose);
    }

    /*页面初始化*/
    function init() {
        getUserInfo();
        bindEvents();
    }
    /*end 页面初始化*/
    init();
};
